package demo;

import java.io.FileNotFoundException;

public class Test {

    public static void fun(int[] array, int index) {
        //抛出异常throw
        if(array == null) {
            throw new NullPointerException();
        }
        System.out.println("空指针异常被执行");//异常被抛出并且捕获后，就不会继续往下执行代码

        if(index > array.length) {
            throw new ArrayIndexOutOfBoundsException();
        }
        System.out.println("数组越界异常被执行");
    }

    public static void main1(String[] args) {

        int[] array = null;
        fun(array, 10);
        
    }




    //throws声明异常
    public static void openConfig(String str) throws FileNotFoundException{

        if(str.equals("config.ini")) {
            throw new FileNotFoundException();//抛出异常
        }

    }


    public static void main(String[] args) {

        try {
            openConfig("config.ini");//try是声明可能会出现的异常
        } catch (FileNotFoundException e){
            System.out.println("已捕获异常，FileNotFoundException,正在处理异常....");
        } finally {
            System.out.println("finally被执行");//finally一直都会被调用执行，所以finally是用来关闭资源的
        }

        
        System.out.println("异常已处理，继续执行下一步...");

    }






}
